/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DatePanel.java
 *
 * Created on Jun 27, 2012, 9:47:23 AM
 */

package com.piza.datepicker;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JLabel;

/**
 *
 * @author cwb
 */
public class DatePanel extends javax.swing.JPanel {

    private static JButton[] datesBtn=new JButton[35];
    private static JLabel[] weeksLbl=new JLabel[7];

    private JButton highlightBtn;
    private Color defColor;
    private Calendar cal=Calendar.getInstance();
    /** Creates new form DatePanel */
    public DatePanel() {
        initComponents();
        weeksLbl[0]=createLabel("Sun");
        weeksLbl[1]=createLabel("Mon");
        weeksLbl[2]=createLabel("Tue");
        weeksLbl[3]=createLabel("Wed");
        weeksLbl[4]=createLabel("Thu");
        weeksLbl[5]=createLabel("Fri");
        weeksLbl[6]=createLabel("Sat");

        for(int i=0;i<weeksLbl.length;i++){
            this.add(weeksLbl[i]);
        }
        for(int j=0;j<35;j++){
            datesBtn[j]=createDateButton(j);
            this.add(datesBtn[j]);
        }

        defColor=datesBtn[0].getBackground();

    }


    public void toToday(){
        
    }
    public Date getSelectedDate(){
        return this.cal.getTime();
    }
    public void updateTime(int year,int month){
        this.cal.set(Calendar.YEAR, year);
        this.cal.set(Calendar.MONTH, month-1);
        for(int j=0;j<35;j++){
           this.remove(datesBtn[j]);
            datesBtn[j]=createDateButton(j);
            this.add(datesBtn[j]);
        }
    }

    private JLabel createLabel(String text){
        JLabel label=new JLabel(text);
        label.setFont(new Font("",Font.BOLD,15));
        label.setHorizontalAlignment(JLabel.CENTER);
        return label;
    }

    private String btnStr(int position){
        String text="";
        cal.set(Calendar.DATE, 1);
          int dateOfWeek=cal.get(Calendar.DAY_OF_WEEK);
          position++;
          if(position>=dateOfWeek){
              int dateOfMonth=(position-dateOfWeek+1);
              int maxDateOfMonth=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
              if(dateOfMonth<=maxDateOfMonth){
                 cal.set(Calendar.DATE, dateOfMonth);
                 text=""+cal.get(Calendar.DAY_OF_MONTH);
              }

          }
        return text;
    }
   


    private JButton createDateButton(int i){
        JButton button=new JButton();
        String text=btnStr(i);
        if(!text.equals("")){
           button.setText(text);
                 button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                       JButton btn=(JButton)e.getSource();
                      highlightBtn(btn);
                        dateBtnAction(btn.getText());
                    }
                });
        }
        return button;
    }

    private void highlightBtn(JButton btn){
        if(this.highlightBtn!=null){
            this.highlightBtn.setBackground(defColor);
            this.highlightBtn=btn;
            btn.setBackground(Color.LIGHT_GRAY);
        }else{
            this.highlightBtn=btn;
            btn.setBackground(Color.LIGHT_GRAY);
        }
    }
    private void dateBtnAction(String date){
        int dateOfMonth=Integer.valueOf(date);
        this.cal.set(Calendar.DAY_OF_MONTH, dateOfMonth);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N
        setLayout(new java.awt.GridLayout(6, 7, 1, 1));
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
